package com.aispeech.audiowavelib.view;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.util.Log;
import android.view.SurfaceHolder;
import android.view.SurfaceView;

/**
 * 该类只是一个初始化surfaceview的封装
 *
 * @author cokus
 */
public class WaveSurfaceView extends SurfaceView implements SurfaceHolder.Callback {

    private final String TAG = "WaveSurfaceView";

    private SurfaceHolder mHolder;
    private int mLineOff;//上下边距距离

    public int getLineOff() {
        return mLineOff;
    }

    public void setLineOff(int lineOff) {
        this.mLineOff = lineOff;
    }

    public WaveSurfaceView(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.mHolder = getHolder();
        mHolder.addCallback(this);
    }

    /**
     * @author cokus init surfaceview
     */
    public void initSurfaceView(final SurfaceView sfv) {
        Log.d(TAG, "initSurfaceView height = " + this.getHeight());

        new Thread() {
            public void run() {
                Canvas canvas = sfv.getHolder().lockCanvas(
                    new Rect(0, 0, sfv.getWidth(), sfv.getHeight()));// 关键:获取画布
                if (canvas == null) {
                    return;
                }
                //canvas.drawColor(Color.rgb(241, 241, 241));// 清除背景
                canvas.drawARGB(255, 239, 239, 239);

                int height = sfv.getHeight() - mLineOff;
                Paint paintLine = new Paint();
                Paint centerLine = new Paint();
                Paint circlePaint = new Paint();
                circlePaint.setColor(Color.rgb(246, 131, 126));
                circlePaint.setAntiAlias(true);

                canvas.drawCircle(0, mLineOff / 4, mLineOff / 4, circlePaint);// 上面小圆
                canvas.drawCircle(0, sfv.getHeight() - mLineOff / 4, mLineOff / 4, circlePaint);// 下面小圆
                canvas.drawLine(0, 0, 0, sfv.getHeight(), circlePaint);//垂直的线
                paintLine.setColor(Color.rgb(169, 169, 169));
                centerLine.setColor(Color.rgb(39, 199, 175));
                canvas.drawLine(0, mLineOff / 2, sfv.getWidth(), mLineOff / 2, paintLine);//最上面的那根线
                canvas.drawLine(0, sfv.getHeight() - mLineOff / 2 - 1, sfv.getWidth(), sfv.getHeight() - mLineOff / 2 - 1, paintLine);//最下面的那根线
//    	         canvas.drawLine(0, height*0.25f+20, sfv.getWidth(),height*0.25f+20, paintLine);//第二根线
//    	         canvas.drawLine(0, height*0.75f+20, sfv.getWidth(),height*0.75f+20, paintLine);//第3根线
                canvas.drawLine(0, height * 0.5f + mLineOff / 2, sfv.getWidth(), height * 0.5f + mLineOff / 2, centerLine);//中心线
                sfv.getHolder().unlockCanvasAndPost(canvas);// 解锁画布，提交画好的图像
            }
        }.start();
    }

    @Override
    public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
        // TODO Auto-generated method stub
    }

    @Override
    public void surfaceCreated(SurfaceHolder holder) {
        initSurfaceView(this);
    }

    @Override
    public void surfaceDestroyed(SurfaceHolder holder) {

    }

}
